package com.cmuis.goodguide;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

/*
 * This activity / page controls the interaction with the barcode scanner application
 * that is downloaded in conjunction with the application. It requests, handles, and sends
 * to GoodGuide the UPC code scanned on the product.
 */
public class barcodescanner extends Activity {
	
	ImageButton buttonScan = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scanner);
		buttonScan = (ImageButton) findViewById(R.id.scan_item);
		
		buttonScan.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				Intent i = new Intent(barcodescanner.this, barcodescanner.class);
				startActivity(i);

			}
		});
		IntentIntegrator.initiateScan(this);
	}

	/* 
	 * Forces the back button to go to the homepage instead of the previous page.
	 * This fixes an annoying bug with passing intents in Android. I think.
	 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	    	Intent i = new Intent(barcodescanner.this, goodguide.class);
			startActivity(i);
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	/*
	 * This simply gets all of the data from the scan that was initiated and puts it into
	 * a Bundle and then triggers the detailed view via intents.
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		IntentResult scan = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
		
		if(scan != null && scan.getContents() != null) {	
			Intent itemIntent = new Intent(getApplicationContext(),DetailsPage.class);
			Bundle b = new Bundle();
			b.putString("productUPC", scan.getContents());
			b.putString("productID", null);
			itemIntent.putExtras(b);
			startActivity(itemIntent);
		}
	}
	
	// Creates the options ability when the menu is created.
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		return true;
	}

	// This section details the menu items and their actions when pressed.
	// This is the main level of navigation in the application.
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.search:
			Intent i = new Intent(this, goodguide.class);
			startActivity(i);
			return true;
		case R.id.barcode_scanner:
			Intent j = new Intent(this, barcodescanner.class);
			startActivity(j);
			return true;
		case R.id.history:
			Intent p = new Intent(this, History.class);
			startActivity(p);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}